﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;



public class Popup_IngameRanking : Popup
{
    private class sortRankingData : IComparer<Hashtable>
    {
        int IComparer<Hashtable>.Compare(Hashtable a, Hashtable b)
        {
            int c1 = (int)a["rank"];
            int c2 = (int)b["rank"];

            if (c1 == 0)
                c1 = 10;
            if (c2 == 0)
                c2 = 10;

            if (c1 > c2)
                return 1;
            if (c1 < c2)
                return -1;
            else
                return 0;
        }
    }

    public UILabel[] m_UserNameLabels;
    public UILabel[] m_UserDeltaPointLabels;
    public UILabel[] m_UserRankingPointLabels;

    //------------------------ 안씀
    public UILabel[] m_UserRankingPlusLabels;
    public UILabel[] m_UserRankingMinusLabels;
    public GameObject[] m_UserRankingUp;
    public GameObject[] m_UserRankingDown;

    public GameObject[] m_Body;                 // 랭킹 바디 루트 (Child)
    public GameObject[] m_RankingSprite;        // 순위 표시 라벨
    public GameObject[] m_MydataBG;             // 유저 아이디일 경우 백그라운드 활성화

    

    public void SetPopupWithData(List<Hashtable> data, int MySid)
    {
        sortRankingData sd = new sortRankingData();
        data.Sort(sd);
        
        int i = 0;        
        foreach (Hashtable tHash in data)
        {
            int rank = (int)tHash["rank"];

            //if (rank > 0)
            {
                int point = (int)tHash["Point"];
                int delta = (int)tHash["Delta"];
                BoardPerson person = tHash["BoardPerson"] as BoardPerson;

                m_UserNameLabels[i].text = person.nickName;

                if(delta > 0)
                    m_UserDeltaPointLabels[i].text = string.Format("+{0}", delta);
                else
                    m_UserDeltaPointLabels[i].text = delta.ToString();

                m_UserRankingPointLabels[i].text = point.ToString();

                m_MydataBG[i].SetActive(person.SID == MySid); // 자기 정보 표시                
            }
            if(rank == 0)
            {                
                m_RankingSprite[i].SetActive(false);
            }
            i++;
        }

        for (int j = i; i < 4; i++)
        {
            m_MydataBG[i].SetActive(false);
            m_Body[i].SetActive(false);
            m_RankingSprite[i].SetActive(false);
        }
    }

    public override void OnOkFunc(GameObject obj)
    {
        if (BoardManager.Instance.boardPerson_me.isRetired)
        {            
            StageManagerWrapper.SendLeave();
        }
        else if (!CSceneManager.isSingleGame)
        {            
            CSceneManager.manager.SendGetRandomBoxInfos();
        }
        else if (CSceneManager.isSingleGame)
        {            
            StageManagerWrapper.SendLeave();
        }
    }


    public override void OnXFunc(GameObject obj)
    {
        if (BoardManager.Instance.boardPerson_me.isRetired)
        {
            StageManagerWrapper.SendLeave();
        }
        else if (!CSceneManager.isSingleGame)
        {
            CSceneManager.manager.SendGetRandomBoxInfos();
        }
        else if (CSceneManager.isSingleGame)
        {
            StageManagerWrapper.SendLeave();
        }
    }
}
